<?php
namespace Home\Controller;
header('Content-Type:text/html;charset=utf-8');
class PersonalCenterController extends AuthController
{

    /* 用户信息 */
    public function index()
    {
        // dump(session('home_user'));
        $this->login_if();//判断是否登录。某些页面必须需要登录.
        $this->assign('user', $user = M('user a')->join('LEFT JOIN dx_degree b ON a.degree=b.degree_id')->join('LEFT JOIN dx_coupon d ON a.coupon=d.coupon_id')->join('LEFT JOIN dx_sex c ON a.sex=c.sex_id')->where('user_name = "' . $_SESSION['home_user']['user_name'] . '"')->find());//用户信息赋值;
        $this->display('personalCenter');
    }

    /* 用户信息编辑 */
    public function personalCenterSave()
    {
        if (IS_POST) {
            $_POST['age'] = date('Y') - $_POST['age'];
            if (!empty($_POST['nickname'])) {    //判断昵称是否为空
                if (strlen(I('post.nickname')) < 24) { //判断昵称长度
                    if (!empty($_FILES['image_id']['type'])) {
                        $upload = new \Think\Upload();// 实例化上传类
                        $upload->maxSize = 3145728;// 设置附件上传大小 3MB
                        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                        $upload->rootPath = './Public/';
                        $upload->savePath = './Uplodes/image_id/'; // 设置附件上传目录    // 上传文件
                        $info = $upload->upload();
                        if (is_file(THINK_PATH . '../Public/' . $_SESSION['home_user']['image_id']) && $_SESSION['home_user']['image_id'] != 'Uplodes/image_id/59424ce0073a5.jpg') {//判断头像的路径是否为文件
                            unlink(THINK_PATH . '../Public/' . $_SESSION['home_user']['image_id']);
                        }
                        $_POST['image_id'] = substr($info['image_id']['savepath'] . $info['image_id']['savename'], 2);//上传头像图片的路径
                        if (!$info) {// 上传错误提示错误信息  头像上传
                            $this->error($upload->getError());
                        }
                    }

                    if (M('user')->where('user_name="' . $_SESSION['home_user']['user_name'] . '"')->save(I('post.'))) {
                        session('home_user', M('user')->where('user_name = "' . $_SESSION['home_user']['user_name'] . '"')->find());//上传成功后重新给session赋值
                        $this->success('修改成功', U('personalCenter/index'));
                    } else {
                        $this->error('修改失败!');
                    }

                } else {
                    $this->error('昵称过长,请输入 9位汉字 或 25位英文字母 以内的昵称!');
                }
            } else {
                $this->error('昵称输入不能为空!');
            }
        }
        $this->login_if();
        $this->assign('user', M('user')->where('user_name = "' . $_SESSION['home_user']['user_name'] . '"')->find());//用户信息赋值;
        $this->assign('degree', M('degree')->select());//学位赋值;
        $this->display();
    }

    /* 用户项目信息 */
    public function projectInformation()
    {
		$this->login_if();
		$session = session('home_user');
		$pro = M('project')->where('user_id='.$session['id'])->count();
		//dump($pro);
		$map['mes_sender'] = $session['id'];
		$map['mes_title'] = "申请通过";
		$map['mes_state'] = 1;
		$mes = M('project a')->join('LEFT JOIN dx_message b ON a.pro_id=b.mes_pro')->where('a.pro_state = 0')->where($map)->count();
		//dump($mes);
		$count =  M('project a')->join('LEFT JOIN dx_message b ON a.pro_id=b.mes_pro')->where('a.pro_state = 1')->where($map)->count();
        //dump($count);
		$this->assign('pro' , $pro);
		$this->assign('count' , $count);
		$this->assign('mes' , $mes);
		$this->assign('id' , $session);
        $this->display();
    }

    /* 接受的项目 */
    public function acceptedItems()
    {
        $this->login_if();
		$pros = M('project');
		$session = session('home_user');
		$map['mes_sender'] = $session['id'];
		$map['mes_title'] = "申请通过";
		$map['mes_state'] = 1;
		//$mes = M('message')->where($map)->select();
		//dump($mes);		
		$count = M('project a')->join('LEFT JOIN dx_message b ON a.pro_id=b.mes_pro')->where('a.pro_state = 0')->where($map)->count();
		//dump($ptt);
		$Page = new \Think\Page($count,3);
        //dump($Page);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');

        $show  =  $this->bootstrap_page_style($Page->show());
		
		$pro =  M('project a')->join('LEFT JOIN dx_message b ON a.pro_id=b.mes_pro')->where('a.pro_state = 0')->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
		//dump($pro);
		$this->assign('pro' , $pro);
		$this->assign('page',$show);
        $this->display();
    }

    /* 接受的项目详情 */
    public function acceptedItems_details($id)
    {
        $this->login_if();		
		$pro =  M('project')->where('pro_id ='.$id)->find();
		$this->assign('pro' , $pro);
        $this->display();
    }

    /* 发布的项目 */
    public function publishedProject()
    {
        $this->login_if();
		$pros = M('project');
		$session = session('home_user');
		$count = $pros->where('user_id='.$session['id'])->count();
		//dump($count);
		$Page = new \Think\Page($count,3);
        //dump($Page);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');

        $show  =  $this->bootstrap_page_style($Page->show());
		$pro = $pros->where('user_id='.$session['id'])->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('pro' , $pro);
		$this->assign('page',$show);
        $this->display();
    }


    /* 发布的项目详情 */
    public function project_details($id)
    {
        $this->login_if();
		$pro =  M('project')->find($id);
		$this->assign('pro' , $pro);
        $this->display();
    }

    /* 完成的项目 */
    public function completeProject()
    {
        $this->login_if();
		$pros = M('project');
		$session = session('home_user');
		$map['mes_sender'] = $session['id'];
		$map['mes_title'] = "申请通过";
		$map['mes_state'] = 1;
		//$mes = M('message')->where($map)->select();
		//dump($mes);
		$count = M('project a')->join('LEFT JOIN dx_message b ON a.pro_id=b.mes_pro')->where('a.pro_state = 1')->where($map)->count();
		//dump($ptt);
		$Page = new \Think\Page($count,3);
        //dump($Page);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');

        $show  =  $this->bootstrap_page_style($Page->show());
		
		$pro =  M('project a')->join('LEFT JOIN dx_message b ON a.pro_id=b.mes_pro')->where('a.pro_state = 1')->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
		//dump($pro);
		$this->assign('pro' , $pro);
		$this->assign('page',$show);
        $this->display();
    }

    /* 完成的项目详情 */
    public function completeProject_details($id)
    {
        $this->login_if();
		$pro =  M('project')->where('pro_id ='.$id)->find();
		$this->assign('pro' , $pro);
        $this->display();
    }

    /* 会员中心 */
    public function memberCenter()
    {
        $this->login_if();
        $this->display();
    }

    /* 系统消息 */
    public function systemMessage()
    {
		$this->login_if();
        $user = M('user')->where('id="' . $_SESSION['home_user']['id'] . '"')->find();
        $array = explode(',', $user['mes_id']);
        $arr = array();
        for ($i = 0; $i < count($array); $i++) {
            $arr[$i] = M('message a')->join('LEFT JOIN dx_user b ON a.mes_sender = b.id')->where('a.mes_id="' . $array[$i] . '"')->find();
            $arr[$i]['mes_id'] = $array[$i];
        }
        $count = count($arr);// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $show = $this->bootstrap_page_style($Page->show());// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $first = $Page->firstRow;
        if (!empty($_GET['p'])) {
            $end = ($Page->listRows) * I('get.p');
        } else {
            $end = 3;
        }
        for ($u = $first; $u < $end; $u++) {
            if (!empty($arr[$u])) {
                $list[] = $arr[$u];
            }
        }
        $this->assign('data', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        
        $this->display();
    }

    /* 系统消息详情 */
    public function systemMessage_details()
    {
        $this->assign('message', M('message a')->join('LEFT JOIN dx_user b ON a.mes_sender=b.id')->where('a.mes_id="' . I('get.mes_id') . '"')->find());
        $this->assign('get',$_GET);
        $this->login_if();
        $this->display();
    }

    public function load_ajax(){
        M('message')->where('mes_id="'.I('post.mes_id').'"')->save(array('mes_state'=>1));
    }

    public function agree_ajax()
    {

        if (I('post.mes_agree') == 1) {
            if (M('message')->add(array('mes_sender' => $_SESSION['home_user']['id'], 'mes_pro' => I('post.mes_pro'), 'mes_title' => '申请通过', 'mes_agree' => I('post.mes_agree'), 'mes_content' => I('post.mes_content'), 'mes_time' => time()))) {
                if (M('message')->where('mes_id="' . I('post.mes_id') . '"')->save(array('mes_agree' => 1))) {
                    $mes_id = M('message')->where('mes_sender="' . $_SESSION['home_user']['id'] . '" AND mes_time="' . time() . '"')->find();
                    $user = M('user')->where('id="' . I('post.user_id') . '"')->find();
                    $user['mes_id'] = trim($user['mes_id'],',');
                    if (M('user')->where('id="' . I('post.user_id') . '"')->save(array('mes_id' => trim($user['mes_id'] . ',' . $mes_id['mes_id'],',')))) {
                        $this->ajaxReturn('已同意 申请通过');
                    }
                }
            }
        } else {
            M('message')->where('mes_id="' . I('post.mes_id') . '"')->save(array('mes_agree' => 2));
            if (M('message')->add(array('mes_sender' => I('post.pro_applicant'), 'mes_pro' => I('post.mes_pro'), 'mes_title' => '申请未通过', 'mes_agree' => I('post.mes_agree'), 'mes_content' => I('post.mes_content'), 'mes_time' => time()))) {
                if (M('message')->where('mes_id="' . I('post.mes_id') . '"')->save(array('mes_agree' => 1))) {
                    $mes_id = M('message')->where('mes_sender="' . $_SESSION['home_user']['id'] . '" AND mes_time="' . time() . '"')->find();
                    $user = M('user')->where('id="' . I('post.user_id') . '"')->find();
                    if (M('user')->where('id="' . I('post.user_id') . '"')->save(array('mes_id' => trim($user['mes_id'] . ',' . $mes_id['mes_id'],',')))) {
                        $this->ajaxReturn('已拒绝 申请未通过');
                    }
                }
            }
        }
    }

    public function user()
    {
        $this->login_if();
        $this->assign('user', M('user')->where('user_name = "' . $_SESSION['home_user']['user_name'] . '"')->find());//用户信息赋值;
        $this->assign('degree', M('degree')->select());//学位赋值;
        $this->display();
    }
	
	public function delsystemMessage($id){
		$pro = M('message');
        $pros = $pro->delete($id);
        if($pros){
            $this->success('删除成功！' , U('PersonalCenter/systemMessage'));
        }else{
            $this->error('删除失败');
        }
	}
}